package main

import (
	"fmt"
	"whoscored/util/httputil"
	"strings"
	"github.com/boltdb/bolt"
	"strconv"
	"whoscored/util/boltdb"
	"encoding/json"
)

/**
抓取比赛
 */
func fetchPlayer(dbKey string,bucket,id string)  {

	url:="http://mapi01.whoscored.com/Matches/Show/"+id
	fmt.Println(url)
	headers:=map[string]string{

	}
	data:=httputil.Request(url,"GET",headers,nil,)
	//fmt.Println(data)
	if !strings.Contains(data,"info"){
		boltdb.Put("failedPlayer",bucket,id,"1")
		return
	}

	boltdb.Put(dbKey,bucket,id,data)
}

func main()  {

	i:=0
	t:=0
	s:=",252-2,108-5,206-4,81-3,74-22,177-21,155-13,182-77,31-95,233-85,225-17,252-7,11-68,45-162,165-41,212-40,81-6,250-12,250-30,247-36,"
	boltdb.ScanTx("matchs-2009", func(tx *bolt.Tx) {

		tx.ForEach(func(name []byte, b *bolt.Bucket) error {

			b.ForEach(func(k, v []byte) error {
				//fmt.Println(string(v))
				m:=make(map[string]interface{},0)

				json.Unmarshal(v,&m)
				s1:=m["region"].([]interface{})
				s2:=m["match"].([]interface{})
				ss:=strconv.FormatFloat(s1[4].(float64), 'f', -1, 64)
				region:=","+s1[1].(string)+"-"+ss+","
				bucket:=strconv.FormatFloat(s2[0].(float64), 'f', -1, 64)
				id:=strconv.FormatFloat(s2[1].(float64), 'f', -1, 64)
				if strings.Contains(s,region){

					fetchPlayer("player",bucket,id)
					//seed:=500+rand.Intn(500)
					//time.Sleep(time.Millisecond*time.Duration(seed))
					i++
				}
				t++
				return nil
			})
			return nil
		})
	})
	fmt.Println(i,t)
}
